/**
 * Created by adophper on 2017/12/31.
 */

$(function () {
    //点击二维码文字显示图片
    $(document).on('click', ".list_qrcode", function(e){
        $(".wCode_mark").addClass('hidden');
        var parents =  $(this).parents($(this).data('parent'));
        parents.find(".wCode_mark").removeClass('hidden');
        if (e.stopPropagation)
            e.stopPropagation();//停止冒泡  非ie
        else
            e.cancelBubble = true;//停止冒泡 ie
    })
    $(".share_wechat_btn").on('click', function (e) {
        var box = $(this).parent().next(".share_wechat");
        if (box.is(":hidden")) {
            box.show();
        }else{
            box.hide();
            return false;
        }
        if (e.stopPropagation)
            e.stopPropagation();//停止冒泡  非ie
        else
            e.cancelBubble = true;//停止冒泡 ie
    })
    $(document).on('click', function (e) {
        $(".wCode_mark").addClass('hidden');
        $(".share_wechat").hide();
    })

    //订阅
    $('#subscribe').click(function () {
        var email = $('#subscribe-email').val();
        var re = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        var _csrf = $('#_csrf').val();
        if (!re.test(email)){
            alert('请输入正确的邮箱格式');
            return false;
        }
        $.post("<?=Yii::$app->urlManager->createUrl(['home/subscribe'])?>", {_csrf:_csrf,email: email}, function(data){

            if(data.code == 0){
                alert("订阅成功");
            }else {
                alert("订阅失败");
            }
        });
    })
    //搜索
    $(".news_search>input,.exhibition_search>input").on('keypress', function (e) {
        var keywords = $(this).val();
        if (e.keyCode == 13 && keywords == "") {
            return false;
        }
        location.href = $(this).next(".btn_red").data('url')+'&keywords='+keywords;
    })
    //资讯搜索
    $(".news_search>.btn_red").on('click', function(e){
        var keywords = $(this).prev("input").val();
        if (keywords == "") {
            return false;
        }
        location.href = $(this).data('url')+'&keywords='+keywords;
    });
    //展会搜索
    $(".exhibition_search>.btn_red").on('click', function(e){
        var keywords = $(this).prev("input").val();
        if (keywords == "") {
            return false;
        }
        location.href = $(this).data('url')+'&keywords='+keywords;
    });

    $(window).scroll(function () {
        var scrollTop = $(window).scrollTop();
        if (scrollTop > 10) {
            $(".rightGoToTop").show();
            $(".fixeMark").addClass('enable');
        }else{
            $(".rightGoToTop").hide();
            $(".fixeMark").removeClass('enable');
        }
    })
})

/**
 定义一个函数，对给定的数分为四类(判断密码类型)，返回十进制1，2，4，8
 数字 0001 -->1  48~57
 小写字母 0010 -->2  97~122
 大写字母 0100 -->4  65~90
 特殊 1000 --> 8 其它
 */
function charType(num){
    if(num >= 48 && num <= 57){
        return 1;
    }
    if (num >= 97 && num <= 122) {
        return 2;
    }
    if (num >= 65 && num <= 90) {
        return 4;
    }
    return 8;
}
/**
 * 判断是否手机号码格式
 * @param mobile
 * @returns {boolean}
 */
function isMobile(mobile) {
    if (!/^1\d{10}$/.test(mobile)) {
        return false;
    }
    return true;
}
/**
 * 判断是否邮箱格式
 * @param email
 * @returns {boolean}
 */
function isEmail(email){
    if (!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,}$/.test(email)) {
        return false;
    }
    return true;
}
/**
 * 获取ajax页面
 * @param url
 * @param div
 * @param data
 */
function getAjaxPage(url, data, div){
    $("#"+div).html('<p>数据加载中，请稍后...</p>');
    $.get(url, data, function(txt) {
        $("#"+div).html(txt);
    })
}

//一键分享
function shareTo(stype, key){
    var ftit = '';
    var flink = '';
    var lk = '';
    ftit = $('.pctitle').text();
    flink = $('.pcdetails img').eq(0).attr('src');
    if(typeof flink == 'undefined'){
        flink='';
    }
    var protocolStr = document.location.protocol+'//';
    if(flink == ''){
        lk = protocolStr+window.location.host+'/static/frontend/image/share_logo.jpg';
    }
    if(flink.indexOf('/uploads/') != -1) {
        lk = protocolStr+window.location.host+flink;
    }
    if(flink.indexOf('ueditor') != -1){
        lk = flink;
    }

    if(stype=='qzone'){
        window.open('https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+document.location.href+'?sharesource=qzone&title='+ftit+'&pics='+lk+'&summary='+document.querySelector('meta[name="description"]').getAttribute('content'));
    }
    if(stype=='sina'){
        window.open('http://service.weibo.com/share/share.php?url='+document.location.href+'?sharesource=weibo&title='+ftit+'&pic='+lk+'&appkey='+key);
    }
    if(stype == 'qq'){
        window.open('http://connect.qq.com/widget/shareqq/index.html?url='+document.location.href+'?sharesource=qzone&title='+ftit+'&pics='+lk+'&summary='+document.querySelector('meta[name="description"]').getAttribute('content')+'&desc=sadasdas');
    }
    if(stype == 'qqweb'){
        window.open('http://share.v.t.qq.com/index.php?c=share&a=index&url='+document.location.href+'&title='+ftit+'&pic='+lk+'&site='+document.location.href+'&appkey='+key);
    }
}
